import { Component, OnInit } from '@angular/core';
import { FormsModule, FormBuilder, FormGroup, Validators } from '@angular/forms';
import { StudentManageService } from "./student-manage.service";
import { Util } from "../../../common/util";
import * as _ from 'lodash';

@Component({
  selector: 'app-student-manage',
  templateUrl: './student-manage.component.html',
  styleUrls: ['./student-manage.component.scss'],
  providers: [StudentManageService]
})
export class StudentManageComponent implements OnInit {
  // 学生信息
  public studentInfo: any = {};
  // 导师信息
  public teachInfo: any = {};
  // 全选
  public checkall: any = false;
  // 教师分页
  public teachPages: any = [];
  // 教师列表
  public teachList: any;
  // 教师查询模版
  public teachSearch: any = {
    TeacherDepartment: "",
    TeacherName: "",
    PageIndex: 1,
    PageSize: 10,
    Total: ""
  }
  // 学生分页
  public pages: any = [];
  // 学生列表
  public studentList: any;
  // 查询模版
  public searchBody: any = {
    StudentId: "",
    StudentGrade: "",
    StudentDepartment: "",
    StudentMajor: "",
    TeacherName: "",
    SelectStatus: "",
    ReadStatus: "",
    Level: "",
    PageIndex: 1,
    PageSize: 10,
    Total: ""
  }
  // 新增模版
  public addStudentModel: any = {
    StudentGrade: "",
    StudentDepartment: "",
    StudentMajor: "",
    StudentId: "",
    StudentName: "",
    StudentSex: "",
    StudentNumber: "",
    StudentEmail: "",

    TeacherName: "",
    SelectStatus: "0",
    ReadStatus: "0",
    Level: "0"
  }
  // 编辑学生信息
  public editStudentModel: any = {};

  constructor(
    public studentService: StudentManageService,
    public util: Util
  ) {
    this.getStudentList();
  }
  ngOnInit() {

  }
  // 获取学生列表
  getStudentList(index?) {
    let that = this;
    if (index) {
      this.searchBody.PageIndex = 1;
    }
    this.studentService.getStudentList(this.searchBody).subscribe(data => {
      if (data) {
        that.studentList = data.list;
        that.searchBody.Total = data.total;
        that.searchBody.PageSize = data.pageSize;
        that.searchBody.PageIndex = data.pageIndex;
        if (data.total) {
          that.pages = that.util.setPage(data.total, data.pageSize, data.pageIndex);
        } else {
          // 无数据返回空页
          that.pages = [1];
        }
        that.checkall = false;
      }
    })
  }
  // 换页
  changePage(type, index) {
    var pageCount = Math.ceil(this.searchBody.Total / this.searchBody.PageSize);
    if (type == 'pre') {
      if (this.searchBody.PageIndex - 1 > 0) {
        this.searchBody.PageIndex = this.searchBody.PageIndex - 1;
        this.getStudentList();
      }
    }
    if (type == 'next') {
      if ((this.searchBody.PageIndex + 1) <= pageCount) {
        this.searchBody.PageIndex = this.searchBody.PageIndex + 1;
        this.getStudentList();
      }
    }
    if (type == 'first') {
      this.searchBody.PageIndex = 1;
      this.getStudentList();
    }
    if (type == 'last') {
      this.searchBody.PageIndex = pageCount;
      this.getStudentList();
    }
    if (type == 'page') {
      this.searchBody.PageIndex = index;
      this.getStudentList();
    }
  }
  // 清空查询模版
  clearSearch() {
    this.searchBody.StudentGrade = "";
    this.searchBody.StudentDepartment = "";
    this.searchBody.StudentMajor = "";
    this.searchBody.StudentId = "";
    this.searchBody.TeacherName = "";
    this.searchBody.ReadStatus = "";
    this.searchBody.SelectStatus = "";
    this.searchBody.Level = "";
  }
  // 新增学生
  addStudent() {
    let that = this;
    this.studentService.addStudent(this.addStudentModel).subscribe(data => {
      if (data) {
        that.studentService.addStudentTask(that.addStudentModel).subscribe(data => {
          if (data) {

          } else {
            window['swal']("失败", "任务书新增失败!", "error");
          }
        })
        window['swal']("成功", "新增成功!", "success");
        window['$']('#addModel').modal('toggle');
        that.getStudentList();
      }
    })
  }
  // 清空新增模版
  clearAddModel() {
    this.addStudentModel = {};
    this.addStudentModel.TeacherName = "";
    this.addStudentModel.SelectStatus = "0";
    this.addStudentModel.ReadStatus = "0";
    this.addStudentModel.Level = "0"
  }
  // 编辑学生信息
  editStudent() {
    let that = this;
    this.studentService.editStudent(this.editStudentModel).subscribe(data => {
      if (data) {
        window['swal']("成功", "更新成功", "success");
        window['$']('#editModel').modal('hide');
        that.getStudentList();
      }
    })
  }
  // 删除学生
  delStudent(student) {
    let that = this;
    window["swal"]({
      title: "注意!",
      text: "是否要删除 " + student.StudentName + " 学生?",
      type: "info",
      confirmButtonText: "确认",
      confirmButtonColor: "#DD6B55",
      cancelButtonText: "取消",
      showCancelButton: true,
      closeOnConfirm: false,
      closeOnCancel: true,
      showLoaderOnConfirm: true
    },
      function (isConfirm) {
        if (isConfirm) {
          that.studentService.delStudent(student.Id).subscribe(data => {
            if (data) {
              window["swal"]("成功", "已删除该学生", "success");
              that.getStudentList();
            }
          })
        }
      })
  }
  // 弹出模版
  toggleModel(formId, info?) {
    if (info) {
      this.editStudentModel = _.clone(info);
    }
    let id = "#" + formId;
    window['$'](id).modal('toggle');
  }
  // 分配导师
  getTeacherList(type, index?) {
    let that = this;
    if (index) {
      this.teachSearch.PageIndex = 1;
    }
    this.studentService.getTeacherList(this.teachSearch).subscribe(data => {
      if (data) {
        that.teachList = data.list;
        that.teachSearch.Total = data.total;
        that.teachSearch.PageSize = data.pageSize;
        that.teachSearch.PageIndex = data.pageIndex;
        if (data.total) {
          that.teachPages = that.util.setPage(data.total, data.pageSize, data.pageIndex);
        } else {
          // 无数据返回空页
          that.teachPages = [1];
        }
        if (type == 'open') {
          that.toggleModel('teachModel');
        }
      }
    })
  }
  // 换页
  teachChangePage(type, index) {
    var pageCount = Math.ceil(this.teachSearch.Total / this.teachSearch.PageSize);
    if (type == 'pre') {
      if (this.teachSearch.PageIndex - 1 > 0) {
        this.teachSearch.PageIndex = this.teachSearch.PageIndex - 1;
        this.getTeacherList(null, null);
      }
    }
    if (type == 'next') {
      if ((this.teachSearch.PageIndex + 1) <= pageCount) {
        this.teachSearch.PageIndex = this.teachSearch.PageIndex + 1;
        this.getTeacherList(null, null);
      }
    }
    if (type == 'first') {
      this.teachSearch.PageIndex = 1;
      this.getTeacherList(null, null);
    }
    if (type == 'last') {
      this.teachSearch.PageIndex = pageCount;
      this.getTeacherList(null, null);
    }
    if (type == 'page') {
      this.teachSearch.PageIndex = index;
      this.getTeacherList(null, null);
    }
  }
  // 清空分配查询模版
  teachClear() {
    this.teachSearch.TeacherName = "";
    this.teachSearch.TeacherDepartment = "";
    this.teachSearch.pageIndex = 1;
  }
  // 全选
  checkAll() {
    if (this.checkall) {
      _.forEach(this.studentList, item => {
        item.isChecked = true;
      })
    } else {
      _.forEach(this.studentList, item => {
        item.isChecked = false;
      })
    }
  }
  // 导师分配
  selectTeach(teach) {
    let that = this;
    let len = window['$']('input[name="checkbox"]:checked');
    if (len.length > 0) {
      window["swal"]({
        title: "确定?",
        text: "确定要将所选学生分配给 " + teach.TeacherName + " 导师?",
        type: "info",
        confirmButtonText: "确认",
        confirmButtonColor: "#DD6B55",
        cancelButtonText: "取消",
        showCancelButton: true,
        closeOnConfirm: false,
        closeOnCancel: true,
        showLoaderOnConfirm: true
      },
        function (isConfirm) {
          if (isConfirm) {
            _.forEach(that.studentList, item => {
              if (item.isChecked) {
                that.studentService.selectTeach(item, teach).subscribe(data => {
                  if (data) {
                    window['$']('#teachModel').modal('hide');
                    window["swal"]("成功", "分配成功!", "success");
                    that.getStudentList();
                  }
                })
              }
            })
          }
        })
    } else {
      window["swal"]("注意", "请勾选要分配的学生!", "warning");
    }
  }
  // 显示学生详情
  showStudent(student, type) {
    this.studentInfo = student;
    if (type == 'stu') {
      this.toggleModel('showStudentDetail');
    }
    if (type == 'teach') {
      this.toggleModel('showTeachDetail');
    }
  }
}
